package easier.window.picker;

import static easier.window.picker.DateTimePickerView.TYPE_YEAR_MONTH_DAY;
import static easier.window.picker.DateTimePickerView.TYPE_YEAR_MONTH_DAY_HOUR_MINUTE;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import java.util.Calendar;

public class CalendarPicker extends DialogFragment {

    protected PickerViewDialog dialog;
    protected ActionListener actionListener;
    protected DateTimePickerView datePicker;

    public void setActionListener( ActionListener actionListener ) {

        this.actionListener = actionListener;
    }

    /**
     * @param context 创建picker
     */
    protected void createPicker( Context context ) {

        dialog = new PickerViewDialog( context );
        dialog.setContentView( R.layout.dialog_date_picker );
        datePicker = dialog.findViewById( R.id.datePicker );
    }

    @NonNull
    @Override
    public Dialog onCreateDialog( @Nullable Bundle savedInstanceState ) {

        if ( dialog == null ) {
            createPicker( getContext() );
        }
        configDateTimePickerView( datePicker );
        View yes = dialog.findViewById( R.id.done );
        View no = dialog.findViewById( R.id.close );
        attachActions( yes, no );
        return dialog;
    }

    /**
     * @param datePicker 配置
     */
    protected void configDateTimePickerView( DateTimePickerView datePicker ) {

    }


    @SuppressWarnings( "StatementWithEmptyBody" )
    protected void attachActions( View done, View cancel ) {

        cancel.setOnClickListener( v -> {
            if ( actionListener != null ) {
                actionListener.onCancelClick();
            }
            dismiss();
        } );
        done.setOnClickListener( v -> {
            if ( actionListener != null ) {

                /* 根据显示类型,清除无用的信息 */
                int type = datePicker.getType();
                Calendar date = datePicker.getSelectedDate();

                if ( type == TYPE_YEAR_MONTH_DAY ) {
                    date.set( Calendar.HOUR, 0 );
                    date.set( Calendar.MINUTE, 0 );
                    date.set( Calendar.SECOND, 0 );
                    date.set( Calendar.MILLISECOND, 0 );
                } else if ( type == TYPE_YEAR_MONTH_DAY_HOUR_MINUTE ) {
                    date.set( Calendar.SECOND, 0 );
                    date.set( Calendar.MILLISECOND, 0 );
                } else {
                    date.set( Calendar.SECOND, 0 );
                    date.set( Calendar.MILLISECOND, 0 );
                }

                actionListener.onDoneClick( date );
            }
            dismiss();
        } );
    }

    public interface ActionListener {

        void onCancelClick();

        void onDoneClick( Calendar calendar );
    }

    /**
     * 设置时间到 当前日期:23:59:59:999
     */
    public static void setCalendarToTimeEnd( Calendar calendar ) {

        calendar.set( calendar.get( Calendar.YEAR ), calendar.get( Calendar.MONTH ), calendar.get( Calendar.DATE ), 23, 59, 59 );
        calendar.set( Calendar.MILLISECOND, 999 );
    }

    /**
     * 设置时间到 当前日期:00:00:00:0
     */
    public static void setCalendarToTimeStart( Calendar calendar ) {

        calendar.set( calendar.get( Calendar.YEAR ), calendar.get( Calendar.MONTH ), calendar.get( Calendar.DATE ), 0, 0, 0 );
        calendar.set( Calendar.MILLISECOND, 0 );
    }
}
